package com.HttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.alibaba.fastjson.JSONObject;

public class InitPeoPle {


	// 调用WS
	public   void initPeo(String userid,String name) throws Exception, IOException { 
		
		String result = connectionTX(userid,name);
		if (result.contains("OK")) {
			System.out.println(userid);
		}else{
			System.out.println(name);
			
		}
		
	}
	// 生成url
		private String createUrl() throws Exception {
		//	  String usersig = createSig(); //写死了 要不太慢了
		 String usersig = "eJxNjU1vgkAQhv-LXm2aZT-C0qQH03pAUENaTPCyQVhxxIV12Zpq0-9eIJJ0bjPPvM-7gz7jj*eurGVuDJToBXkMY*yxgPvoaYTq24BVMj84ZQfOOSf9y4OOMZk7Se2Qns4OtBplPuOECTzJoFSNgwOMKqhyMy81NJOrKNqvxkl3M*qfq4Oq31aL9C1M3mN-w9IsS4-F-aj1RpNApxFZnS2b2Q4iQecBJfclZbMkrNYXHohduof6nFxPpbjs210Ud3679W6K8VbUlQ4XfJutxeuj7Kp6Tdv0hQR73CMUD4N*-wAPjVhr";
			String identifier = "igapAdmin";
			long sdkappid =1400014957;
			String random = getFixLenthString();
			String url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import?usersig=" + usersig
					+ "&identifier=" + identifier + "&sdkappid=" + sdkappid + "&random=" + random + "&contenttype=json";

			return url;
		}
	
	/**
	 * 与TX连接
	 * 
	 * @return
	 */
	private String connectionTX(String userid,String username) throws Exception {
		String urlstr = createUrl();
		Map map = new HashMap<String, String>();
		map.put("Identifier", "identifier_" + userid);
		map.put("Nick", username);
		JSONObject json=new JSONObject();
		String sendMsg = json.toJSONString(map);

		// 开启HTTP连接
		URL url = new URL(urlstr);
		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

		// 设置HTTP请求相关信息
		httpConn.setRequestProperty("Content-Length", String.valueOf(sendMsg.getBytes("UTF-8").length));
		httpConn.setRequestProperty("Content-Type", "application/json");
		httpConn.setRequestMethod("POST");

		httpConn.setDoOutput(true);
		httpConn.setDoInput(true);

		// 进行HTTP请求
		OutputStream outObject = httpConn.getOutputStream();
		outObject.write(sendMsg.getBytes("UTF-8"));

		// 关闭输出流
		outObject.close();

		// 获取HTTP响应数据
		InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(), "utf-8");
		BufferedReader inReader = new BufferedReader(isr);
		StringBuffer result = new StringBuffer();
		String inputLine;
		while ((inputLine = inReader.readLine()) != null) {
			result.append(inputLine);
		}

		// 打印HTTP响应数据
		// System.out.println(result);

		// 关闭输入流
		inReader.close();
		isr.close();
		return result.toString();

	}
	
	
	// 生成随机数
	private static String getFixLenthString() {

		String result = "";
		for (int i = 0; i < 6; i++) {
			Random rm = new Random();
			double pross = (1 + rm.nextDouble()) * Math.pow(10, 6);
			String fixLenthString = String.valueOf(pross);

			result = result + fixLenthString;
		}

		result = result.replace(".", "").substring(0, 32);
		return result;
	}

	// 测试主方法
	public static void main(String[] args) throws IOException, Exception {
		 
	}
}
